package cn.gogpay.dcb.ixc.cloud.auth.utils;


import cn.gogpay.dcb.ixc.cloud.auth.dto.AuthDTO;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: YinHeng
 * @date: 2021/2/7 11:08
 * @description:
 */
public class AuthContextStore {
    private static final ThreadLocal<AuthDTO> authStore = new ThreadLocal<>();

    /**
     * 设置当前用户
     *
     * @param authDTO
     */
    public static void put(AuthDTO authDTO) {
        authStore.set(authDTO);
    }

    /**
     * 获取当前用户
     *
     * @return
     */
    public static AuthDTO get() {
        return authStore.get();
    }

    /**
     * 清楚当前用户
     */
    public static void clean() {
        authStore.remove();
    }

    /**
     * 获取当前用户数据权限
     *
     * @return
     */
    public static List<Long> getDataScopes() {
        AuthDTO authDTO = authStore.get();
        if (authDTO == null) {
            return new ArrayList<>();
        }
        return authDTO.getDataScopes();
    }

    /**
     * 检查是否拥有指定机构的数据权限
     *
     * @param orgId
     * @return
     */
    public static Boolean hasDataScopes(Long orgId) {
        for (Long id : getDataScopes()) {
            if (id.equals(orgId)) {
                return true;
            }
        }
        return false;
    }

}
